package com.dailyyoga.res;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

import org.xmlpull.v1.XmlPullParser;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;

class YogaFileRes extends YogaRes {

	final static String PATH = Environment.getExternalStorageDirectory().getPath() + ".dailyyoga/plugs/";

	public YogaFileRes(String lang) {
		mLang = lang;
	}

	public String getString(String plugName, String key) {

		String content = null;
		try {
			FileInputStream fileInputStream = new FileInputStream(PATH + mLang
					+ "/" + plugName + "/strings.xml");

			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(fileInputStream, "utf-8");
			abc: for (int evenType = parser.getEventType(); evenType != XmlPullParser.END_DOCUMENT; evenType = parser
					.next()) {

				switch (evenType) {
				case XmlPullParser.START_TAG:

					String name = parser.getName();

					if (name.equals("string")) {

						if (parser.getAttributeCount() > 0) {

							String value = parser.getAttributeValue(0);
							Log.d("string", "name =" + name + " value=" + value
									+ "key=" + key);

							if (value.equals(key)) {

								content = parser.nextText()
										.replace("\\n", "\n");
								Log.d("string", content + "");
								break abc;
							}

						}

					}
				}
			}

			fileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.gc();

		return content;
	}

	public Bitmap getBitMap(String plugName, String key) {

		return BitmapFactory.decodeFile(PATH + mLang + "/" + plugName + "/"
				+ key);
	}

	public FileDescriptorEx getPlayAudioPath(String plugName, String key) {

		try {

			String path = PATH + mLang + "/" + plugName + "/" + key;

		

			Log.d("path", "path=" + path);

			@SuppressWarnings("resource")
			RandomAccessFile rf = new RandomAccessFile(path, "rw");

			FileDescriptor fd = rf.getFD();

			FileDescriptorEx assetFileDescriptor = new FileDescriptorEx();

			assetFileDescriptor.mFileDescriptor = fd;
			assetFileDescriptor.mStart = 0;
			assetFileDescriptor.mLength = rf.length();
			return assetFileDescriptor;

		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;
	}

	public FileDescriptorEx getVideoPath(String plugName, String key) {

		try {
			@SuppressWarnings("resource")
			RandomAccessFile rf = new RandomAccessFile(PATH +mLang+"/"+ plugName + "/"
					+ key, "r");

			FileDescriptorEx assetFileDescriptor = new FileDescriptorEx();
			assetFileDescriptor.mLength = rf.length();
			assetFileDescriptor.mFileDescriptor = rf.getFD();
			assetFileDescriptor.mStart = 0;

			return assetFileDescriptor;

		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;
	}

	public boolean isInstallPlugs(String plugName) {

		return getPlugVc(plugName) > 0;
	}

	public int getPlugVc(String plugName) {
		int vc = -1;
		try {

			String str = "";
			FileInputStream in = new FileInputStream(PATH +mLang+"/"+ plugName + "/"
					+ "vc.txt");
			// size 为字串的长度 ，这里一次性读完
			int size = in.available();
			byte[] buffer = new byte[size];
			in.read(buffer);
			in.close();
			str = new String(buffer, "utf-8");
			vc = Integer.parseInt(str.trim());

			Log.d("vc", plugName + "vc=" + vc);

		} catch (Exception e) {
			e.printStackTrace();

		}

		return vc;
	}

	public InputStream getParamXMl(String plugName, String praramKey)
			throws IOException {

		FileInputStream fileInputStream = new FileInputStream(PATH +mLang+"/"+ plugName
				+ "/" + praramKey);

		return fileInputStream;
	}

	@Override
	public Bitmap getPrompt(String plugName, String key) {
		return BitmapFactory.decodeFile(PATH +mLang+"/"+ plugName + "/" + key);
	}

}
